package g11.testdao;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import g11.actionutil.AppointmentUtil;
import g11.actionutil.AppointmentUtilImpl;
import g11.dao.AppointmentDAO;
import g11.dao.AppointmentDAOImpl;
import g11.dao.DAOException;
import g11.dao.EmployeeDAO;
import g11.dao.EmployeeDAOImpl;
import g11.dao.PatientDAO;
import g11.dao.PatientDAOImpl;
import g11.model.Appointment;
import g11.model.Employee;
import g11.model.Patient;

import java.sql.Date;

import org.junit.Test;

public class testAppointmentDAO {

	@Test
	@SuppressWarnings("deprecation")
	public void test() throws DAOException {

		Employee employee = new Employee();
		employee.setUsername("penny");
		employee.setPassword("1234");
		Byte type = 1;
		employee.setType(type);
		employee.setName("Penny Money");
		employee.setDepartment("Surgery");
		employee.setTitle("Surgery Doctor");
		EmployeeDAO employeeDao = new EmployeeDAOImpl();
		employeeDao.createEmployee(employee);
		Employee retrievedEmployee = new Employee();
		retrievedEmployee = employeeDao.getEmployeeByUsername("penny");

		// create a patient
		Patient patient = new Patient();
		patient.setUsername("jhope");
		patient.setName("Julia Hope");
		patient.setPassword("1234");
		type = 0;
		patient.setType(type);
		patient.setPhone("519-888-8888");
		patient.setAddress("77 Weber Street");
		patient.setOhip("2222222222ka");
		patient.setSin("1111111111");
		patient.setCurrentHealth("Diabetes.");
		Date date = new Date(111, 12, 8);
		java.sql.Date myDate = new java.sql.Date(date.getTime());
		patient.setLastVisitTime(myDate);
		System.out.println(myDate.toString());
		PatientDAO patientDao = new PatientDAOImpl();
		patientDao.createPatient(patient);
		Patient retrievedPatient = new Patient();
		retrievedPatient = patientDao.getPatientByOhip(patient.getOhip());

		// create a appointment for this doctor and this patient
		Appointment appointment = new Appointment();
		appointment.setDoctor(retrievedEmployee);
		appointment.setPatient(retrievedPatient);
		// set date and time for this appointment: set date to Mar 1st,2012
		Date tempDate = new Date(112, 3, 1);
		appointment.setAppointmentDate(tempDate);
		appointment.setStartTime("9:30");
		appointment.setEndTime("9:45");
		// set status to pending
		byte status = 0;
		appointment.setStatus(status);
		appointment.setLocation("room 123");
		// test to persist the appointment,createAppointment(appointment)
		AppointmentDAO appointmentDao = new AppointmentDAOImpl();
		appointmentDao.createAppointment(appointment);
		Appointment retrievedAppointment = new Appointment();
		// test appointmentDao.getAppointmentByDoctor(retrievedEmployee)
		retrievedAppointment = appointmentDao.getPendingAppointmentByDoctor(
				retrievedEmployee).get(0);
		assertEquals(retrievedEmployee, retrievedAppointment.getDoctor());
		assertEquals(retrievedEmployee.getName(), retrievedAppointment
				.getDoctor().getName());
		assertEquals("room 123", retrievedAppointment.getLocation());

		// test updateAppointment and getAppointmentByPatient(retrievedPatient)
		retrievedAppointment.setEndTime("10:00:00");
		appointmentDao.updateAppointment(retrievedAppointment);
		retrievedAppointment = appointmentDao.getAppointmentByPatient(
				retrievedPatient).get(0);
		assertEquals("10:00:00", retrievedAppointment.getEndTime());

		// make second appointment appointment1 and set status=1(visited)
		Appointment appointment1 = new Appointment();
		appointment1.setDoctor(retrievedEmployee);
		appointment1.setPatient(retrievedPatient);
		// set date and time for this appointment: set date to Mar 1st,2012
		tempDate = new Date(112, 3, 1);
		appointment1.setAppointmentDate(tempDate);
		appointment1.setStartTime("10:30");
		appointment1.setEndTime("10:45");
		// set status to pending
		status = 1;
		appointment1.setStatus(status);
		appointmentDao.createAppointment(appointment1);

		// make third appointment appointment2 and set status=0(pending)
		Appointment appointment2 = new Appointment();
		appointment2.setDoctor(retrievedEmployee);
		appointment2.setPatient(retrievedPatient);
		// set date and time for this appointment: set date to Mar 1st,2012
		tempDate = new Date(112, 3, 1);
		appointment2.setAppointmentDate(tempDate);
		appointment2.setStartTime("10:15");
		appointment2.setEndTime("10:45");
		// set status to pending
		status = 0;
		appointment2.setStatus(status);
		// don't persist this appointment yet: need check conflict first, if no
		// conflict, then persist
		// appointmentDao.createAppointment(appointment2);

		// check if conflicts
		AppointmentUtil appUtil = new AppointmentUtilImpl();
		assertTrue(appUtil.checkAppointmentConflicByDoctor(retrievedEmployee,
				appointment2,true));

		// delete the appointments and the doc and patient
		appointmentDao.deleteAppointment(retrievedAppointment);
		// appointmentDao.deleteAppointment(appointment2);
		appointmentDao.deleteAppointment(appointment1);
		patientDao.deletePatient(retrievedPatient);
		employeeDao.deleteEmployee(retrievedEmployee);

	}

}
